package unitTests;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.awt.Color;

import javax.swing.JTextField;

import mainpackage.Category;
import mainpackage.CategoryNameNotUniqueException;
import mainpackage.DataManager;
import mainpackage.NoColorsLeftException;
import mainpackage.Todo;

import org.junit.Test;

import com.sun.xml.internal.bind.v2.TODO;

import ui.AddCategoryPresenter;
import ui.AddCategoryWindow;
import ui.MainPresenter;
import ui.MainWindow;


import static org.mockito.Mockito.*;

public class AddCategoryPresenterTest {
	
	AddCategoryWindow mockedAddCategoryWindow = mock(AddCategoryWindow.class);
	AddCategoryPresenter addCategoryPresenter = new AddCategoryPresenter(mockedAddCategoryWindow);
	DataManager mockedDataManager = mock(DataManager.class);
	
	MainPresenter mockedMainPresenter = mock(MainPresenter.class);

	public AddCategoryPresenterTest(){
		addCategoryPresenter.setDataManager(mockedDataManager);	
		when(mockedAddCategoryWindow.getTitleField()).thenReturn(new JTextField());
	}
	
	@Test
	
	public void categoryIsAdded() throws Exception {
		String name = "category";
		when(mockedAddCategoryWindow.getCategoryName()).thenReturn(name);
		addCategoryPresenter.setDataManager(mockedDataManager);
		addCategoryPresenter.setMainPresenter(mockedMainPresenter);
		
		addCategoryPresenter.saveButtonClicked();
	
		mockedMainPresenter.addCategoryToList(mockedDataManager.getCategoryByName(name));
		verify(mockedAddCategoryWindow).dispose();
		verify(mockedDataManager).addCategory(name);
	}


	@Test

	
	public void categoryIsNotAddedWhenNameIsEmpty() throws Exception {
		String name = null;
		when(mockedAddCategoryWindow.getCategoryName()).thenReturn(name);
		
		addCategoryPresenter.saveButtonClicked();
		
		verify(mockedAddCategoryWindow).getTitleField();
	}
	
	@Test
	
	public void categoryIsNotAddedWhenNameAlreadyExists() throws Exception {
		String name = "category";
		when(mockedAddCategoryWindow.getCategoryName()).thenReturn(name);
		String sameName = "category";
		when(mockedAddCategoryWindow.getCategoryName()).thenReturn(sameName);
		
		addCategoryPresenter.setDataManager(mockedDataManager);
		addCategoryPresenter.setMainPresenter(mockedMainPresenter);
		
		addCategoryPresenter.saveButtonClicked();
		mockedMainPresenter.addCategoryToList(mockedDataManager.getCategoryByName(name));
		verify(mockedDataManager).addCategory(name);
		
		try {
			mockedDataManager.addCategory(sameName);
		} catch (CategoryNameNotUniqueException e) {
			verify(mockedAddCategoryWindow).getTitleField();
		}
	

	}
	
}
